var headurl = `https://techcherry.cn/jzgapi/`
//`https://bwv.net.cn/${__wxConfig.envVersion == 'trial' ? 'prod' :'api'}`;
var imgurl = `${headurl}imgs/`, audiourl = `${headurl}audio/`
class api {

  getheadurl() {
    return headurl;
  }

  getimgurl() {
    return imgurl;
  }

  getaudiourl() {
    return audiourl;
  }
  /**
   * 登录
   * code- string, 小程序调用login生成的code(临时登录凭证)
   */
  wxlogin(code, success, fail) {
    fstpostdata('/login/wxlogin', {
      jscode: code
    }, res => success(res), res => fail(res))
  }
  uploadimg(data, params) {
     console.log(data,params,"q11fesfds");
    // return;
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0, //上传失败的个数
      pics = data.pics ? data.pics : [];
    // console.log(data.path[i]);
    //console.log(data.path[i], 'uploadimg--data.path[i]data.path[i]');
    if (data.path[i].indexOf(imgurl) != -1) {
      pics.push(data.path[i].replace(imgurl, ''));
    }
    wx.uploadFile({
      header: {
        'content-type': "application/x-www-form-urlencoded",
        token: getApp().logininfo.Token,
        userid: getApp().logininfo.userid
      },
      url:  headurl+'upload/wx_upload',
      filePath: data.path[i],
      name: 'upload', //这里根据自己的实际情况改
      formData: null, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1

        let result = JSON.parse(resp.data);
        pics.push(result.path);
        console.log(pics, "picssss")
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
      },
      fail: (res) => {
        console.log(res, 'failres');
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: (res) => {
        console.log('complete',{i,res});
        i++; //这个图片执行完上传后，开始上传下一张            
        if (i == data.path.length) { //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
          params.success(pics);

        } else { //若图片还没有传完，则继续调用函数                
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          data.pics = pics;
          that.uploadimg(data, params);
        }
      }
    });
  }
  uploadimg2(data, params) {
    // console.log(data,params,"q11fesfds");
    // return;
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0, //上传失败的个数
      pics = data.pics ? data.pics : [];
    // console.log(data.path[i]);
    console.log(data.path[i], 'uploadimg--data.path[i]data.path[i]');
    if (data.path[i].indexOf(imgurl) != -1) {
      pics.push(data.path[i].replace(imgurl, ''));
    }
    wx.uploadFile({
      header: {
        'content-type': "application/x-www-form-urlencoded",
        token: getApp().logininfo.Token,
        userid: getApp().logininfo.userid
      },
      url: data.url,
      filePath: data.path[i],
      name: 'upload', //这里根据自己的实际情况改
      formData: null, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1

        let result = JSON.parse(resp.data);
        pics.push(result.path);
        console.log(pics, "picssss")
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
      },
      fail: (res) => {
        console.log(res, 'failres');
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++; //这个图片执行完上传后，开始上传下一张            
        if (i == data.path.length) { //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
          params.success(pics);

        } else { //若图片还没有传完，则继续调用函数                
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          data.pics = pics;
          that.uploadimg(data, params);
        }
      }
    });
  }
  // 保存头像昵称vvv
  updateuser(data, succ) {
    postdata('/Users/setdateUser', data, res => succ(res))
  }
  // 保存手机号
  getuserphone(phone, succ) {
    postdata('/Users/setUserphone', { phone }, res => succ(res))
  }
  // 保存手机号
  getphone(data, succ) {
    postdata('/Users/gete', data, res => succ(res))
  }
  //  1.获取轮播列表
  getSwiperlist(succ) {
    postdata('/Swiper/ft_getlist', {}, res => succ(res))
  }
  // 机械分类一级列表
  getMechanicstypelist(succ) {
    postdata('/Mechanicstype/ft_getlist', {}, res => succ(res))
  }
  // 机械分类二级列表
  getsubMechanicstypelist(succ) {
    postdata('/Mechanicstype/ft_getsublist', {}, res => succ(res))
  }
  // 1.获取省
  getprovincelist(succ) {
    postdata('/TB_District/ft_getprovincelist', {}, res => succ(res))
  }
  // 2.获取所有市
  getallcitylist(succ) {
    postdata('/TB_District/ft_getallcitylist', {}, res => succ(res))
  }
  // 1.新建招工
  addRecruit(data, succ) {
    postdata('/Recruit/add', data, res => succ(res))
  }
  // 2.前端获取我的招工列表
  getmyRecruitlist(data, succ) {
    postdata('/Recruit/ft_getmylist', data, res => succ(res))
  }
  // 3.前端获取劳务招工列表
  getallRecruitlist(data, succ) {
    postdata('/Recruit/ft_getlist', data, res => succ(res))
  }
  // 4.招工详情
  getRecruitdetail(id, succ) {
    postdata('/Recruit/getdetail', { id }, res => succ(res))
  }
  // 5.前端获取急聘列表
  geturgentRecruitlist(data, succ) {
    postdata('/Recruit/ft_getlistbyurgent', data, res => succ(res))
  }
  // 6.前端获取管理招工列表
  getworktypeRecruitlist(data, succ) {
    postdata('/Recruit/ft_getlistbyworktype', data, res => succ(res))
  }
  // 1.新建
  addSettledteam(data, succ) {
    postdata('/Settledteam/add', data, res => succ(res))
  }
  //公司类型
  getCompanytype(succ) {
    postdata('/Companytype/ft_getlist', {}, res => succ(res))
  }
  // 1.入驻公司
  addSettledcompany(data, succ) {
    postdata('/Settledcompany/add', data, res => succ(res))
  }
  // 1.设置
  getBasedetail(type, succ) {
    postdata('/Base/getdetail', { type }, res => succ(res))
  }
  // 2.用户实名申请
  addRealnameApply(data, succ) {
    postdata('/Realname/add',data, res => succ(res))
  }



  //购买优惠券 微信支付
  wxpayCoupon(data, succ, fail) {
    data['openid'] = wx.getStorageSync('openid')
    postdata('/Couponorders/puycoupon', data, res => {
      var data = JSON.parse(res.data.res);
      var orderid = data.orderid;
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonce_str,
        package: data.package,
        paySign: data.paySign,
        signType: 'MD5',
        success: res => {
          console.log(res, 'succ')
          if (res.errMsg.indexOf('ok') > -1) {
            succ && succ({ succ: 1, orderid })
          }
        },
        fail: res => {
          //支付取消
          succ && succ({ succ: -1 })
        }
      })
    })
  }

  //充值
  // userrecharge(price,succ) {
  //   postdata('/Orders/userrecharge',{price}, res => succ(res))
  // }
  userrecharge(data, succ, fail) {
    data['openid'] = wx.getStorageSync('openid')
    postdata('/Orders/userrecharge', data, res => {
      var data = JSON.parse(res.data.res);
      var orderid = data.orderid;
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonce_str,
        package: data.package,
        paySign: data.paySign,
        signType: 'MD5',
        success: res => {
          console.log(res, 'succ')
          if (res.errMsg.indexOf('ok') > -1) {
            succ && succ({ succ: 1, orderid })
          }
        },
        fail: res => {
          //支付取消
          succ && succ({ succ: -1 })
        }
      })
    })
  }


  parentingpay(data, succ, fail) {
    data['openid'] = wx.getStorageSync('openid')
    postdata('/order/parentingpay', data, res => {
      var data = JSON.parse(res.data.res);
      var orderid = data.orderid;
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonce_str,
        package: data.package,
        paySign: data.paySign,
        signType: 'MD5',
        success: res => {
          console.log(res, 'succ')
          if (res.errMsg.indexOf('ok') > -1) {
            succ && succ({ succ: 1, orderid })
          }
        },
        fail: res => {
          //支付取消
          succ && succ({ succ: -1 })
        }
      })
    })
  }
  // uploadimg(data, params) {
  //   // console.log(data,params,"q11fesfds");
  //   // return;

  //   var that = this,
  //     i = data.i ? data.i : 0, //当前上传的哪张图片
  //     success = data.success ? data.success : 0, //上传成功的个数
  //     fail = data.fail ? data.fail : 0, //上传失败的个数
  //     pics = data.pics ? data.pics : [];
  //   // console.log(data.path[i]);
  //   wx.uploadFile({
  //     header: {
  //       'content-type': "application/x-www-form-urlencoded",
  //       token: getApp().logininfo.Token,
  //       userid: getApp().logininfo.userid
  //     },
  //     url: data.url,
  //     filePath: data.path[i],
  //     name: 'upload', //这里根据自己的实际情况改
  //     formData: null, //这里是上传图片时一起上传的数据
  //     success: (resp) => {
  //       success++; //图片上传成功，图片上传成功的变量+1

  //       let result = JSON.parse(resp.data);
  //       pics.push(result.path);
  //       console.log(pics, "picssss")
  //       //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
  //     },
  //     fail: (res) => {
  //       fail++; //图片上传失败，图片上传失败的变量+1
  //       console.log('fail:' + i + "fail:" + fail);
  //     },
  //     complete: () => {
  //       console.log(i);

  //       i++; //这个图片执行完上传后，开始上传下一张            
  //       if (i == data.path.length) { //当图片传完时，停止调用          
  //         console.log('执行完毕');
  //         console.log('成功：' + success + " 失败：" + fail);

  //         params.success(pics);

  //       } else { //若图片还没有传完，则继续调用函数                
  //         console.log(i);
  //         data.i = i;
  //         data.success = success;
  //         data.fail = fail;
  //         data.pics = pics;
  //         that.uploadimg(data, params);
  //       }
  //     }
  //   });
  // }
  prepay(data, succ, fail) {
    data['openid'] = wx.getStorageSync('openid')
    postdata('/orders/pay', data, res => {
      var data = JSON.parse(res.data.res);
      var orderid = data.orderid;
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonce_str,
        package: data.package,
        paySign: data.paySign,
        signType: 'MD5',
        success: res => {
          console.log(res, 'succ')
          if (res.errMsg.indexOf('ok') > -1) {
            succ && succ({ succ: 1, orderid })
          }
        },
        fail: res => {
          //支付取消
          succ && succ({ succ: -1 })
        }
      })
    })
  }
}

function postdata(urlparms, data, success, fail) {
  console.log(getApp(), 'getApp().logininfo');
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
      token: getApp().logininfo.Token,
      userid: getApp().logininfo.userid,
      loginid: 1
      // loginid:getApp().logininfo.loginid
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function docpostdata(urlparms, imgsrc, success, fail) {
  wx.request({
    url: 'https://ocr.tencentcloudapi.com/?Action=GeneralEfficientOCR&ImageUrl=' + imgsrc,
    method: "POST",
    // data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded"
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function getdata(urlparms, data, success, fail) {

  wx.request({
    url: headurl + urlparms,
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
      'userId': getApp().userinfo.userId,
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function fstpostdata(urlparms, data, success, fail) {
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}
module.exports = api